package com.zhiyou.app.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.zhiyou.app.common.constant.UserHandler;
import com.zhiyou.app.common.exception.BizException;
import com.zhiyou.app.common.vo.resp.Response;
import com.zhiyou.app.pojo.entity.UserWalletEntity;
import com.zhiyou.app.pojo.enums.WalletTypeEnum;
import com.zhiyou.app.pojo.vo.*;
import com.zhiyou.app.service.IUserWalletService;
import com.zhiyou.app.service.IWalletHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jonathan.z
 * @since 2025-07-03
 */
@RestController
@RequestMapping("/userWallet")
@Api(tags = "用户钱包管理")
public class UserWalletController {
    @Resource
    private IUserWalletService userWalletService;
    @Resource
    private IWalletHistoryService walletHistoryService;
    @Value("${wallet.withdrawal.min:1000}")
    private Integer withdrawalMin;


    @ApiOperation(value = "获取我的钱包")
    @GetMapping("/getMyUserWallet")
    public Response<UserWalletVo> getMyUserWallet() {
        UserWalletEntity one = userWalletService.getOne(new LambdaQueryWrapper<UserWalletEntity>().eq(UserWalletEntity::getUserId, UserHandler.getLoginUser().getUserId()));
        return Response.successWithData(userWalletService.parseWalletVo(one));
    }


    @ApiOperation(value = "获取我的钱包明细")
    @PostMapping("/getMyUserWalletDetail")
    public Response<PageInfo<WalletDetailVo>> getMyUserWalletDetail(@RequestBody WalletDetailRequestVo walletDetailRequestVo) {
        return Response.successWithData(walletHistoryService.getWalletHistory(walletDetailRequestVo.setWalletType(WalletTypeEnum.USER).setUserId(null)));
    }

    @ApiOperation(value = "获取用户钱包明细")
    @PostMapping("/getUserWalletDetail")
    public Response<PageInfo<WalletDetailVo>> getUserWallet(@RequestBody WalletDetailRequestVo walletDetailRequestVo) {
        if (walletDetailRequestVo.getUserId() == null) {
            throw new BizException("查询钱包用户ID不存在");
        }
        return Response.successWithData(walletHistoryService.getWalletHistory(walletDetailRequestVo.setWalletType(WalletTypeEnum.USER)));
    }

    @ApiOperation(value = "钱包提现")
    @PostMapping("/withdrawal")
    public Response<WithdrawalResponseVo> withdrawal(@RequestParam("amount") String amount) {
        Integer withdrawalAmount = Integer.parseInt(amount);
        return Response.successWithData(userWalletService.withdrawal(withdrawalAmount));
    }

    @ApiOperation(value = "用户提现记录")
    @PostMapping("/getWithdrawalList")
    public Response<PageInfo<WithdrawalListResponseVo>> withdrawalList(@RequestBody WithdrawalListQueryRequestVo withdrawalListQueryRequestVo) {
        return Response.successWithData(userWalletService.queryWithdrawalList(withdrawalListQueryRequestVo));
    }

    @ApiOperation(value = "我的提现记录")
    @PostMapping("/getMyWithdrawalList")
    public Response<PageInfo<WithdrawalListResponseVo>> myWithdrawalList(@RequestBody WithdrawalListQueryRequestVo withdrawalListQueryRequestVo) {
        withdrawalListQueryRequestVo.setUserId(UserHandler.getLoginUser().getUserId());
        return Response.successWithData(userWalletService.queryWithdrawalList(withdrawalListQueryRequestVo));
    }

    @ApiOperation(value = "重新执行提现")
    @PostMapping("/reWithdrawal")
    public Response<WithdrawalResponseVo> withdrawalList(@RequestParam Integer withdrawalId) {
        return Response.successWithData(userWalletService.reWithdrawal(withdrawalId));
    }
}
